﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ShopCart
/// </summary>
public class MyShopCart
{
  public List<ProductShop> ListProduct { get; set; }

  public MyShopCart()
  {
    ListProduct = new List<ProductShop>();
  }

  public void Add(ProductShop product)
  {
    if (!CheckExits(product.Id))
    {
      ListProduct.Add(product);
    }
    else
    {
      for (int i = 0; i < ListProduct.Count; i++)
      {
        if (ListProduct[i].Id == product.Id)
        {
            ListProduct[i].Quantity += product.Quantity;
          break;
        }
      }
    }
  }

  public void Delete(int id)
  {
    ProductShop item = ListProduct.Where(p => p.Id == id).FirstOrDefault();
    if (item != null)
      ListProduct.Remove(item);    
  }

  public void AddQuantity(int id)
  {
    ProductShop item = ListProduct.Where(p => p.Id == id).FirstOrDefault();
    if (item != null)
      item.Quantity++;
  }

  public void UpdateQuantity(int id, int quantity)
  {
    ProductShop item = ListProduct.Where(p => p.Id == id).FirstOrDefault();
    if (item != null)
      item.Quantity = quantity;
  }

  private bool CheckExits(int id)
  {
    foreach (ProductShop product in ListProduct)
    {
      if (product.Id == id)
        return true;
    }
    return false;
  }

  public decimal GetTotalCost()
  {
    decimal cost = 0;
    foreach (ProductShop product in ListProduct)
    {
      cost += (product.Cost * product.Quantity);
    }
    return cost;
  }

  public int GetTotalProduct()
  {
    int total = 0;
    foreach (ProductShop product in ListProduct)
    {
      total += product.Quantity;
    }
    return total;
  }
}